1510K - King's Task - CodeForces Solution


brute force graphs implementation *1200

Please click on ads to support us..

Python Code:

def func1(a,n):

    
    for i in range(n):
        a[i],a[i+n]=a[i+n],a[i]

    return a

    
def func2(a,n):

    i=1
    for _ in range(n):
        a[i],a[i-1]=a[i-1],a[i]
        i+=2

    return a





n=int(input())
l=list(map(int,input().split()))
a=l[:]
temp=l[:]


mn=10**9
for j in range(2):
    ans=0
    a=l[:]
    while a[-1]!=2*n:

        if j:
            a=func2(a,n)

        else:
            a=func1(a,n)

        ans+=1
        j=not j

    if a==sorted(l):
        mn=min(mn,ans)


if mn==10**9:
    print(-1)

else:
    print(mn)
    
    
           


    

    


    

C++ Code:

// #include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <unordered_map>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <unordered_set>
#include <cstring>
#include <climits>
#include <algorithm>
using namespace std;
#define endl "\n"
#define int long long int
#define transform_to_upper(s)  transform(s.begin(), s.end(),s.begin(),::toupper);
#define transform_to_lower(s) transform(s.begin(), s.end(),s.begin(),::tolower);


void fun(vector<int> & v){
  int n=v.size();
  for(int i=0;i<n;i+=2)swap(v[i],v[i+1]);
    return ;
}


void fun2(vector<int> & v){
  int n=v.size();
  for(int i=0,j=n/2;j<n;i++,j++)swap(v[i],v[j]);

    return ;
}

bool check(vector<int>a,vector<int>  b){
  for(int i=0;i<a.size();i++){
    if(a[i]!=b[i])return false;
  }
  return true;
}



signed main(){
    int n;
    cin>>n;
    n*=2;

    vector<int> v(n);
    for(int & val : v)cin>>val;
   int ans1=-1,ans2=-1;

  vector<int> one=v,two=v;
  sort(v.begin(),v.end());

    int operations=0;
    while(operations<n){
      if(check(v,one))break;
          if(operations&1) fun(one);
          else fun2(one);
          operations++;
    }
   if(check(one,v)) ans1=operations;
    operations=0;


    while(operations<n){
      if(check(v,two))break;
          if(operations&1) fun2(two);
          else fun(two);
          operations++;
    }

   if(check(two,v)) ans2=operations;
   if(ans1==-1||ans2==-1)cout<<max(ans1,ans2)<<endl;
   else cout<<min(ans1,ans2)<<endl;
  



}


Comments

Submit
0 Comments
More Questions

1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password
1646B - Quality vs Quantity
80A - Panoramix's Prediction
1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals
1569C - Jury Meeting
108A - Palindromic Times
46A - Ball Game
114A - Cifera
776A - A Serial Killer
25B - Phone numbers
1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+